home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Turnbull China Bikeride
/
Turnbull China Bikeride - Disc 2.iso
/
ACORNUSERS
/
EMULATOR
/
EMUL6502
/
Sources
/
BubSort
next >
Wrap
Text File
|
1998-08-27
|
2KB
|
69 lines
;Alain BROBECKER (aka baah/Arm's Tech), 27aug1998
;Bubble sort routine from the '6502 programming' book by Rodnay ZAKS
;
;Copies the program and sort it using bubble sort and post-indexed indirect
;adressing. Once done, we check the table is correctly sorted and in such
;case the program ends with the 'brk' inside code. If table is unsorted,
;program ends by executing the 'brk'=&00 at adress 0.
#name BubSortX
#list
#base &100-4 ;Start assembly here
#rw &100 ;Load adress
#rw &100 ;Exec adress
;Zero page adresses used as storage
#set tabpointer = &20
;At first initialise tabpointer, then copy the program in table
.start
lda #table AND &ff ;Initialise tabpointer
sta z,tabpointer
lda #table>>8
sta z,tabpointer+1
ldx #table-start ;Program size
stx table
.copy_one
lda start-1,x
sta table,x
dex:bne copy_one
;Perform bubble sort on table (sorting a program ,wow!)
.BubSort
ldx #0 ;x=exchanged boolean
lda (tabpointer,x)
tay ;y=nb of elements
.BubSortLoop
lda (tabpointer),y ;read table(y)
dey
beq BubSortEnd
cmp (tabpointer),y ;compare to table(y-1)
bcs BubSortLoop
.BubSortExchange
tax ;Exchange elts
lda (tabpointer),y
iny
sta (tabpointer),y
txa
dey
sta (tabpointer),y
ldx #1 ;exchanged=1
bne BubSortLoop ;Get next elt
.BubSortEnd
txa ;Shift exchanged to a register, for compare
bne BubSort ;If some exchanges made, do another pass
;Make sure table is sorted.
ldx table
.CheckSorted
lda table,x
dex
beq SortedOk
cmp table,x
bcs CheckSorted
jmp 0
.SortedOk
brk
.table